package spikes.keyboard;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JComponent;

@SuppressWarnings("serial")
class Key extends JComponent implements MouseListener, Observer {

	private String key;
	private String capsKey;
	protected NarwhalKeyboard keyboard;
	private String active;
	
	public Key(NarwhalKeyboard keyboard, String key, String capsKey) {
		this.keyboard = keyboard;
		this.key = key;
		this.capsKey = capsKey;
		active = key;

		addMouseListener(this);
	}

	@Override
	public void paint(Graphics g) {
		int width = getBounds().width;
		int height = getBounds().height;
		g.setColor(Color.BLACK);
		g.drawRect(0, 0, width - 1, height - 1);
		g.drawString(active, 20, 20);
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		keyboard.pressKey(active);
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		
	}

	@Override
	public void mouseExited(MouseEvent e) {

	}

	@Override
	public void mousePressed(MouseEvent e) {

	}

	@Override
	public void mouseReleased(MouseEvent e) {

	}

	@Override
	public void update(Observable o, Object arg) {
		if (keyboard.isCaps()) {
			active = capsKey;
		} else {
			active = key;
		}
		repaint();
	}
}
